import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 存储动态路由表的
export const asyncRoutes = [
  { path: '/employees',
    component: Layout,
    children: [
      // 设置二级理由的组件中的内容是显示在父组件中
      {
        path: '', // 默认自动匹配父路由
        name: 'employees',
        component: () => import ('@/views/employees/employees'),
        meta: { title: '员工管理', icon: 'example' }
      }
    ]
  },
  { path: '/departments',
    component: Layout,
    children: [
      // 设置二级理由的组件中的内容是显示在父组件中
      {
        path: '', // 默认自动匹配父路由
        name: 'departments',
        component: () => import ('@/views/departments/departments'),
        meta: { title: '组织架构', icon: 'tree' }
      }
    ]
  },
  { path: '/settings',
    component: Layout,
    children: [
      // 设置二级理由的组件中的内容是显示在父组件中
      {
        path: '', // 默认自动匹配父路由
        name: 'settings',
        component: () => import ('@/views/settings/settings'),
        meta: { title: '公司设置', icon: 'table' }
      }
    ]
  },
  { path: '/salarys',
    component: Layout,
    children: [
      // 设置二级理由的组件中的内容是显示在父组件中
      {
        path: '', // 默认自动匹配父路由
        name: 'salarys',
        component: () => import ('@/views/salarys/salarys'),
        meta: { title: '工资管理', icon: 'form' }
      }
    ]
  },
  { path: '/social_securitys',
    component: Layout,
    children: [
      // 设置二级理由的组件中的内容是显示在父组件中
      {
        path: '', // 默认自动匹配父路由
        name: 'social_securitys',
        component: () => import ('@/views/social_securitys/social_securitys'),
        meta: { title: '社保管理', icon: 'user' }
      }
    ]
  },
  { path: '/attendances',
    component: Layout,
    children: [
      // 设置二级理由的组件中的内容是显示在父组件中
      {
        path: '', // 默认自动匹配父路由
        name: 'attendances',
        component: () => import ('@/views/attendances/attendances'),
        meta: { title: '考勤管理', icon: 'nested' }
      }
    ]
  },
  { path: '/approvals',
    component: Layout,
    children: [
      // 设置二级理由的组件中的内容是显示在父组件中
      {
        path: '', // 默认自动匹配父路由
        name: 'approvals',
        component: () => import ('@/views/approvals/approvals'),
        meta: { title: '审批管理', icon: 'table' }
      }
    ]
  },
  { path: '/permissions',
    component: Layout,
    children: [
      // 设置二级理由的组件中的内容是显示在父组件中
      {
        path: '', // 默认自动匹配父路由
        name: 'permissions',
        component: () => import ('@/views/permissions/permissions'),
        meta: { title: '权限管理', icon: 'nested' }
      }
    ]
  }
]
// 配置路由规则,静态路由表(不需要权限可以直接访问)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 合并静态路由和动态路由
  routes: [...asyncRoutes, ...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
